package com.wang.controller;


import com.wang.pojo.User;
import com.wang.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class UserController {
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }


    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){



        User user=userService.selectByName(username);

        if(user.getPassword().equals(password)){
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            return "allbooks";
        }
        return "";
    }

    @RequestMapping("/uInfo")
    @ResponseBody
    public String uInfo(String username){


        String msg="";
        User user=userService.selectByName(username);
        System.out.println(user);

        if(user!=null) msg="true";
        else msg="false";
        System.out.println(msg);

        return msg;
    }

    //转到注册页面
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }


    //注册页面
    @RequestMapping("/register")
    public String register(String username,String password){

        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.insertUser(user);

        return "login";
    }

}
